package org.lk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lk.reggie.common.CustomException;
import org.lk.reggie.entity.Category;
import org.lk.reggie.entity.Dish;
import org.lk.reggie.entity.Setmeal;
import org.lk.reggie.mapper.CategoryMapper;
import org.lk.reggie.service.CategoryService;
import org.lk.reggie.service.DishService;
import org.lk.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    @Autowired
    private CategoryService categoryService;

    /**
     * 根据Id 删除分类。 删除之前要进行校验此分类是否关联了套餐或菜品
     * @param id
     * @return
     */
    @Override
    public boolean myRemoveById(Long id)
    {
        // 查询当前分类是否已经关联了菜品 已关联抛异常
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish:: getCategoryId, id);
        int dishCount = dishService.count(dishQueryWrapper);
        if (dishCount > 0)
        {
            // 抛出异常
            throw new CustomException("该分类下已经关联了菜品");
        }
        // 查询当前分类是否已经关联了套餐 已关联抛异常
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal :: getCategoryId, id);
        int count = setmealService.count(setmealQueryWrapper);
        if (count > 0)
        {
            // 抛异常
            throw new CustomException("该分类下已经关联了套餐");
        }
        // 正常删除
        boolean b = categoryService.removeById(id);
        if (b)
            return true;
        return false;
    }
}
